package  
{
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.geom.Vector3D;
	
	/**
	 * ...
	 * @author Thiago Gomes da Silva
	 */
	public class Player extends MovieClip 
	{
		private const GRAVITY:Number = 10;
		private var speedX:Number;
		private var speedY:Number;
		private var main:Main;
		private var direction:Vector3D;
		private var speed:Vector3D;
		private var right:Boolean = false;
		private var left:Boolean = false;
		private var up:Boolean = false;
		private var down:Boolean = false;
		private var hitEnemy:Boolean = false;
		private var countFrames:int;
		
		
		
		public function Player(speed:Vector3D, main:Main, direction:Vector3D) 
		{
			this.speed = speed;
			this.main = main;
			this.direction = direction;
			this.speed.y = GRAVITY;
			this.countFrames = 0;
			
			Main.getInstance().stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey, false, 0, true)
			Main.getInstance().stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey, false, 0, true)
		}
		
		//Comandos do jogador
		private function pressKey(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.RIGHT)
			{
				right = true;
			}
			if (e.keyCode == Keyboard.LEFT)
			{
				left = true;
			}
			if (e.keyCode == Keyboard.UP)
			{
				up = true;
			}
			if (e.keyCode == Keyboard.DOWN)
			{
				down = true;
			}
		}
		
		private function releaseKey(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.RIGHT)
			{
				right = false;
			}
			if (e.keyCode == Keyboard.LEFT)
			{
				left = false;
			}
			if (e.keyCode == Keyboard.UP)
			{
				up = false;
			}
			if (e.keyCode == Keyboard.DOWN)
			{
				down = false;
			}
		}
		
		public function getSpeedX():Number
		{
			return speedX;
		}
		
		public function getSpeed():Vector3D
		{
			return speed;
		}
		
		public function setSpeedX(v:Number):void
		{
			this.speedX = v;
		}
		
		public function setSpeed(s:Vector3D):void
		{
			this.speed = s;
		}
		
		public function getDirection():Vector3D
		{
			return direction
		}
		
		public function setDirection(d:Vector3D):void
		{
			this.direction = d;
		}
		
		public function handleInput()
		{
			if (right)
			{
				direction.x = 1;
			}
			else if (left)
			{
				direction.x = -1;
			}
			else if (up)
			{
				direction.y = -1;
			}
			else if (down)
			{
				direction.y = 1;
			}
		}
		
		private function movePlayer()
		{
			
			handleInput()
			this.x += direction.x * speed.x;
			
			if (hitEnemy == false)
			{
				this.y += direction.y * speed.y;
			}
			else
			{
				if (countFrames >= main.stage.frameRate/2)
				{
					hitEnemy = false;
					countFrames = 0;
				}
				else
				{
					countFrames++;
					this.y += direction.y * -speed.y;
				}
			}
		}
		
		public function hasHitEnemy()
		{
			if (hitEnemy == false)
			{
				hitEnemy = true;
			}	
		}
		
		public function isMoving():Boolean
		{
			return direction.x != 0 || direction.y != 0;
		}
		
		public function update()
		{
			movePlayer();
			if (this.x + this.width >= this.main.stage.stageWidth)
			{
				this.x = this.main.stage.stageWidth - this.width;
			}
			if (this.x <= 55)
			{
				this.x = 55;
			}
			
		}
		
		
	}

}